package com.example.moneybook.utils;

import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


/**
 * @Author: 包
 * @Date: 2023/2/18 23:04
 * Description:
 */
public class LastDateUtils {
    public String getLastDay(String billDate, int past) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse(billDate);
        calendar.setTime(date);
        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE)-past);
        Date res = calendar.getTime();
        return sdf.format(res);
    }

    public String getLastMonth(String billDate, int ammount) throws ParseException {
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM");
        Date date = sdf1.parse(billDate);
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MONTH, -ammount);
        return sdf1.format(c.getTime());
    }

}
